package cn.edu.lcvc.ebuy_ruanjian2.web.controller.backstage;

import cn.edu.lcvc.ebuy_ruanjian2.service.AdminService;
import cn.edu.lcvc.ebuy_ruanjian2.model.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginLogoutController {

    //相当于
    @Autowired
    private AdminService adminService;

    @RequestMapping(value = "/backstage/login")
    public String login(String username,String password,HttpSession session){
        //判断账号名和密码是否正确
        if (adminService.login(username,password)){//如果登录成功
            //将该用户在数据库的完整信息传递到jsp页面
            Admin admin= adminService.getAdmin(username);
            session.setAttribute("admin",admin);
            //跳转到后台管理系统界面首页
            return "redirect:/jsp/backstage/main.html";
        }else{//登录失败
            //返回登录界面
            return "redirect:/jsp/backstage/login.jsp";
        }
    }

    @RequestMapping(value = "/backstage/logout")
    public String logout(HttpSession session){
        //1.注销用户
        session.removeAttribute("admin");
        //2.跳转到登录界面
        return "redirect:/jsp/backstage/login.jsp";

    }
}
